package com.dhcc.sdc.cdss.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ICdssStandardMatchService;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sdc.cdss.dao.SdcCdssExamMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssExamEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssExamService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 表数据服务层接口
 *
 * @ClassName: TSdcCdssExamServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 11:16:55
 */
@Service("sdcCdssExamService")
public class SdcCdssExamServiceImpl extends BaseServiceImpl<SdcCdssExamMapper, SdcCdssExamEntity> implements ISdcCdssExamService {

    @Autowired
    private ISdcQcService sdcQcService;
    @Autowired
    private ISdsStandSourceService standSourceService;
    @Autowired
    private ISdsStandSourceFieldService sdsStandSourceFieldService;
    @Autowired
    private ICdssStandardMatchService cdssStandardMatchService;
    @Autowired
    private ISdcQcCdssLinkService sdcQcCdssLinkService;

    @Override
    public List<SdcCdssExamEntity> page(Page<SdcCdssExamEntity> page, Map<String, Object> map) {
        List<SdcCdssExamEntity> page1 = this.baseMapper.page(page, map);
        for (SdcCdssExamEntity cdssExamEntity : page1){
            SdcQcCdssLinkEntity sdcQcCdssLinkEntity = sdcQcCdssLinkService.selectByNodeId(cdssExamEntity.getNodeId());
            if (!StringUtils.isEmpty(sdcQcCdssLinkEntity)){
                SdcQcEntity sdcQcEntity = sdcQcService.selectById(sdcQcCdssLinkEntity.getQcItemId());
                if (!StringUtils.isEmpty(sdcQcEntity)){
                    cdssExamEntity.setQcDesc(sdcQcEntity.getQcDesc());
                }
            }
            if (!StringUtils.isEmpty(cdssExamEntity.getDataSource()) && !StringUtils.isEmpty(cdssExamEntity.getDataSourceItem())){
                /*SdsStandSourceEntity sourceEntity = standSourceService.selectById(cdssExamEntity.getDataSource());
                SdsStandSourceFieldEntity sourceFieldEntity = sdsStandSourceFieldService.selectById(cdssExamEntity.getDataSourceItem());
                if (!StringUtils.isEmpty(sourceEntity) && !StringUtils.isEmpty(sourceFieldEntity)){
                    cdssExamEntity.setReferenceTime(sourceEntity.getSourceDesc() + "-" + sourceFieldEntity.getFieldDesc());
                }*/
                String referenceTime = cdssStandardMatchService.handleReferenceTime(SdcCdssExamEntity.class, cdssExamEntity);
                cdssExamEntity.setReferenceTime(referenceTime);
            }
        }
        return page1;
    }

    @Override
    public List<SdcCdssExamEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<Map<String, Object>> sdcItemList(Map<String, Object> map) {
        return this.baseMapper.sdcItemList(map);
    }

    @Override
    public List<String> selectNodeIds(Long entityId) {
        return this.baseMapper.selectNodeIds(entityId);
    }

    @Override
    public void deleteByNodeIds(List<String> nodeIds) {
        this.baseMapper.deleteByNodeIds(nodeIds);
    }
}
